

local LuaMongo = _G.lua_mongo
local CommonDB = require("common.CommonDB")
local Util = require("common.Util")
local ObjHuman = require("core.ObjHuman")
local CombatVideo = require("combat.CombatVideo")
local RoleDBLogic = require("role.RoleDBLogic")
local RoleLogic = require("role.RoleLogic")
local CombatLogic = require("combat.CombatLogic")
local CombatDefine = require("combat.CombatDefine")
local CombatPosLogic = require("combat.CombatPosLogic")
local MoZhuExcel = require("excel.mozhu")
local Util = require("common.Util")

MOZHU_DB = MOZHU_DB or {}

function initAfterStart()
	if _G.is_middle ~= true then return end
	print("  initAfterStart   initAfterStart")
	MOZHU_DB = CommonDB.getValueByKey(CommonDB.KEY_CIYUAN_MOZHU) or {}

	if not MOZHU_DB.time then
		initMoZhu()
	end
end

MOZHU_CITIAO_RARE_MAX = nil
-- 随机 次元魔蛛 词条
function randomCiTiao()
	local weekTime = Util.getWeekStartTime(os.time())
	if MOZHU_DB.citiaoTime and MOZHU_DB.citiaoTime == weekTime then
		return
	end
	MOZHU_DB.citiao = {}
    if not MOZHU_CITIAO_RARE_MAX then
        -- 防止策划删减 词条
        MOZHU_CITIAO_RARE_MAX = 0
        for k, v in pairs(MoZhuExcel.citiao) do
            if v then
                MOZHU_CITIAO_RARE_MAX = MOZHU_CITIAO_RARE_MAX + v.quanzhong
            end
        end
    end

    local getRare = 0
    for i = 1 , 1 do 
        local random = math.random(1, MOZHU_CITIAO_RARE_MAX - getRare)
        for k, v in pairs(MoZhuExcel.citiao) do
            if not MOZHU_DB.citiao[k] and random <= v.quanzhong then
                MOZHU_DB.citiao[k] = i
                getRare = getRare + v.quanzhong
                break
            elseif not MOZHU_DB.citiao[k] then
                -- 防止循环最后一个是 已经随机过的  导致 随机不出来
                random = random - v.quanzhong
            end
        end
    end    
	MOZHU_DB.citiaoTime = weekTime
    return MOZHU_DB.citiao
end

function initMoZhu()
    MOZHU_DB.time = os.time()
    MOZHU_DB.unionRank = {}
    MOZHU_DB.roleRank = {}
    MOZHU_DB.role = {}
    MOZHU_DB.union= {}
	randomCiTiao()

	saveMoZhu()
end

function getMoZhu()
	if not MOZHU_DB or MOZHU_DB == {} then
		MOZHU_DB = CommonDB.getValueByKey(CommonDB.KEY_CIYUAN_MOZHU) or {}
	end
    return MOZHU_DB
end

function saveMoZhu()
	CommonDB.updateValue(CommonDB.KEY_CIYUAN_MOZHU, MOZHU_DB)
--	Util.printTable(MOZHU_DB)
end


function updateHurt(uuid, roleBase, maxHurt)
	local isChange = false
	if not MOZHU_DB.role[uuid] then
		MOZHU_DB.role[uuid] = MOZHU_DB.role[uuid] or {}
		MOZHU_DB.role[uuid].roleBase = roleBase
		MOZHU_DB.role[uuid].hurt = maxHurt
		MOZHU_DB.roleRank[#MOZHU_DB.roleRank + 1 ] = uuid
		isChange = true
	else
		if MOZHU_DB.role[uuid].hurt < maxHurt then
			MOZHU_DB.role[uuid].hurt = maxHurt
			isChange = true
		end
	end

	if isChange then
		refresRoleRank()
	end
	return isChange
end

function updateUnion(unionUuid, unionBase, isChange)
	if not unionUuid or unionUuid == "" then return end
	if not MOZHU_DB.union[unionUuid] then
		MOZHU_DB.union[unionUuid] = MOZHU_DB.union[unionUuid] or {}
		MOZHU_DB.union[unionUuid].unionBase = unionBase
		MOZHU_DB.union[unionUuid].time = os.time()
		MOZHU_DB.unionRank[#MOZHU_DB.unionRank + 1] = unionUuid	
	end
	local unionHurt = 0
	for _, db in pairs(MOZHU_DB.role) do
		if db.roleBase and db.roleBase.unionUuid == unionUuid then
			unionHurt = unionHurt + db.hurt
		end
	end
	MOZHU_DB.union[unionUuid].hurt = unionHurt
	MOZHU_DB.union[unionUuid].time = os.time() 	
	refreshUnionRank()
end

function chageRoleUnionUuid(uuid, unionUuid)
	if not MOZHU_DB.role[uuid] then
		return 
	end

	MOZHU_DB.role[uuid].roleBase.unionUuid = unionUuid
end


function refreshUnion(unionUuid, notRefresh) 
	if not MOZHU_DB.union[unionUuid] then
		return 
	end

	local unionHurt = 0
	for _, db in pairs(MOZHU_DB.role) do
		if db.roleBase and db.roleBase.unionUuid == unionUuid then
			unionHurt = unionHurt + db.hurt
		end
	end
	MOZHU_DB.union[unionUuid].hurt = unionHurt
	if notRefresh then		
	else
		refreshUnionRank()
	end
end  

function sortUnion(a, b)
	print("  a , b ", a , b )
	local unionA = MOZHU_DB.union[a] or 0
	local unionB = MOZHU_DB.union[b] or 0
	if unionA.hurt > unionB.hurt then
		return true
	elseif unionA.hurt == unionB.hurt then
		return unionA.time < unionB.time
	end
end

function refreshAllUnion()
	
	for unionUuid in pairs(MOZHU_DB.union) do
		refreshUnion(unionUuid, true)
	end
	refreshUnionRank()
end

function refreshUnionRank()
--	Util.printTable( MOZHU_DB.unionRank)
	table.sort( MOZHU_DB.unionRank, sortUnion )
end

function sortRole(a, b)
	local roleA = MOZHU_DB.role[a]
	local roleB = MOZHU_DB.role[b]
	return roleA.hurt > roleB.hurt
end

function refresRoleRank()
	table.sort( MOZHU_DB.roleRank, sortRole)
end

function getRoleRank(uuid)
	if not MOZHU_DB.role[uuid] then
		return 0,0
	end

	for k, v in ipairs(MOZHU_DB.roleRank) do
		if v == uuid then
			return k, MOZHU_DB.role[uuid].hurt
		end
	end
	return 0,0
end

function getUnionRank(unionUuid)
	if not unionUuid or unionUuid == "" then
		return 0
	end

	for k, v in ipairs(MOZHU_DB.unionRank) do
		if v == unionUuid then
			return k
		end
	end
	return 0
end

function dismissUnion(unionUuid)
	local rank = getUnionRank(unionUuid)
	if rank > 0 then
		table.remove( MOZHU_DB.unionRank, rank)
	end
	
	if MOZHU_DB.union[unionUuid] then
		MOZHU_DB.union[unionUuid] = nil
	end
	saveMoZhu()
end

function delRole(uuid)
	if MOZHU_DB.role and  MOZHU_DB.role[uuid] then
		local rank = getRoleRank(uuid)
		local role = MOZHU_DB.role[uuid]
		if rank > 0 and role.hurt >= 50000000 then
			table.remove( MOZHU_DB.roleRank, rank)
			MOZHU_DB.role[uuid] = nil

			refreshUnion(role.roleBase.unionUuid)
		end
	end
end


function checkUnion()
	for index, unionUuid in ipairs(MOZHU_DB.unionRank) do
		if unionUuid == "" then
			table.remove( MOZHU_DB.unionRank, index)
			if MOZHU_DB.union[""] then
				MOZHU_DB.union[""] = nil
			end
			saveMoZhu()
			break
		end
	end
end